package com.wan37.logic.email.util;

import com.wan37.logic.component.LogicWorkerHandlerCenter;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.email.domain.vo.AttachmentVO;
import com.wan37.logic.email.domain.vo.EmailVO;
import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameEmail;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import org.springframework.stereotype.Component;

import javax.persistence.Column;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-28 15:03
 **/
@Component
public class EmailRoutes {
    /**
     * 邮箱回复消息
     * @param code
     * @param emailVOList
     * @param userId
     */
    public void sendEmailResponse(int code, List<EmailVO> emailVOList, String userId){
        GameEmail.EmailResponse.Builder erb = GameEmail.EmailResponse.newBuilder();

        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.EMAILRESPONSE, getEmailResponse(code, erb, emailVOList));
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 发送邮件消息
     * @param code
     * @param emailVOList
     * @param receiverIdList
     */
    public void sendEmailBroadcast(int code, List<EmailVO> emailVOList, List<String> receiverIdList) {
        GameEmail.EmailBroadcast.Builder ebb = GameEmail.EmailBroadcast.newBuilder();
        for (String receiverId : receiverIdList){
            ebb.addReceiverId(GameEmail.StringMsg.newBuilder().setStringId(receiverId).build());
        }
        GameEmail.EmailResponse.Builder erb = GameEmail.EmailResponse.newBuilder();
        ebb.setEmailResponse(getEmailResponse(code, erb, emailVOList));
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Gate,
                "", PtoNum.EMAILBROADCAST, ebb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public GameEmail.EmailResponse getEmailResponse(int code,
                                                    GameEmail.EmailResponse.Builder erb,
                                                    List<EmailVO> emailVOList){
        erb.setCode(code);
        if (emailVOList != null){
            for (EmailVO emailVO : emailVOList){
                GameEmail.Email.Builder eb = GameEmail.Email.newBuilder();
                eb.setSenderId(emailVO.getSenderId());
                eb.setContent(emailVO.getContent());
                eb.setCreateTime(emailVO.getCreateTime());
                eb.setEmailId(emailVO.getEmailId());
                eb.setIsRead(emailVO.getIsRead());
                if (emailVO.getAttachmentVOList() != null){
                    for (AttachmentVO attachmentVO : emailVO.getAttachmentVOList()){
                        GameEmail.Attachment.Builder ab = GameEmail.Attachment.newBuilder();
                        ab.setType(attachmentVO.getType());
                        ab.setNumber(attachmentVO.getNumber());
                        ab.setStringId(attachmentVO.getBackpackId());
                        ab.setStrongLevel(attachmentVO.getStrongLevel());
                        eb.addAttachment(ab.build());
                    }
                }
                erb.addEmail(eb.build());
            }
        }
        return erb.build();
    }
}
